#include <iostream>
using namespace std;

#include <sys/types.h>
#include <sys/socket.h>
#include <cstring>
#include <arpa/inet.h>
#include <unistd.h>
#include <stdio.h>
#include <json-c/json.h>
#include "stdShared.h"

#define SERVER_PORT 8888
#define SIZE        20
#define BUFFERSIZE 128

void signUpLoginInterFace()
{

    cout << "1.注册" << endl;
    cout << "2.登录" << endl;
}

/* 为了保存对应的账号和信息，应该打包发送 */
struct userInfo
{
    int type;   //1表示注册，2表示登录
    char name[SIZE];
    char passwd[SIZE];
};

/* 注册函数 */
void registerInfo(int sockfd)
{
    string username;
    cout << "请输入账号:";
    cin >> username;

    string password;
    cout << "请输入密码:";
    cin >> password;
#if 0
    userInfo info;
    memset(&info, 0, sizeof(info));
    info.type = 1;
    strncpy(info.name, username.c_str(), sizeof(info.name) - 1);
    strncpy(info.passwd, password.c_str(), sizeof(info.passwd) - 1);

    /* 发送到服务器 */
    write(sockfd, &info, sizeof(info));
#endif
    /* {} 叫做json对象 */
    /* 1.创建json对象 */
    json_object * jsonObj = json_object_new_object();

    /* 2.向json对象添加<key,value>键值对 */
    json_object_object_add(jsonObj, "type", json_object_new_int(REGISTER));
    json_object_object_add(jsonObj, "name", json_object_new_string(username.c_str()));
    json_object_object_add(jsonObj, "passwd", json_object_new_string(password.c_str()));

    /* 3.将json对象转化为字符串 */
    const char * jsonStr = json_object_to_json_string(jsonObj);
    cout << "jsonStr" << jsonStr << endl;

    /* 4.发送到服务器 */
    write(sockfd, jsonStr, strlen(jsonStr));
}

/* 登录函数 */
void loginInfo(int sockfd)
{

    string username;
    cout << "请输入账号:";
    cin >> username;

    string password;
    cout << "请输入密码:";
    cin >> password;
#if 0 
    userInfo info;
    memset(&info, 0, sizeof(info));
    info.type = 2;
    strncpy(info.name, username.c_str(), sizeof(info.name) - 1);
    strncpy(info.passwd, password.c_str(), sizeof(info.passwd) - 1);

    /* 发送到服务器 */
    write(sockfd, &info, sizeof(info));
#endif

    /* {} 叫做json对象 */
    /* 1.创建json对象 */
    json_object * jsonObj = json_object_new_object();

    /* 2.向json对象添加<key,value>键值对 */
    json_object_object_add(jsonObj, "type", json_object_new_int(LOGIN));
    json_object_object_add(jsonObj, "name", json_object_new_string(username.c_str()));
    json_object_object_add(jsonObj, "passwd", json_object_new_string(password.c_str()));

    /* 3.将json对象转化为字符串 */
    const char * jsonStr = json_object_to_json_string(jsonObj);
    cout << "jsonStr" << jsonStr << endl;

    /* 4.发送到服务器 */
    write(sockfd, jsonStr, strlen(jsonStr));


}

int main()
{
    /* 创建套接字 */
    int sockfd =  socket(AF_INET, SOCK_STREAM, 0);
    if(sockfd == -1)
    {
        perror("sockfd error:");
        exit(-1);
    }
    cout << "sockfd = " << sockfd << endl;

    /* 连接服务器 */
    struct sockaddr_in serverAddr;
    serverAddr.sin_family = AF_INET;
    serverAddr.sin_port = htons(SERVER_PORT);
    const char * serverIp = "192.168.152.128";
    inet_pton(AF_INET, serverIp, &(serverAddr.sin_addr.s_addr));

    int ret = connect(sockfd, reinterpret_cast<struct sockaddr*>(&serverAddr), sizeof(serverAddr));
    if(ret != 0)
    {
        perror("connect error:");
        exit(-1);
    }
    cout << "服务器连接成功" << endl;

    signUpLoginInterFace();

    char readBuffer[BUFFERSIZE];
    int choice = 0;
    while (1)
    {
        memset(readBuffer, 0, BUFFERSIZE);

        cout << "请输入你的选择：";
        cin >> choice;

        switch (choice)
        {
        case 1:
            registerInfo(sockfd);
            continue;

        case 2:
            loginInfo(sockfd);
            break;
        
        default:
            break;
        }
        
        int readBytes = read(sockfd, readBuffer, BUFFERSIZE);
        if(readBytes == 0)
        {
            cout << "已退出连接!" << endl;
            break;
        }
        //cout << "readBuffer = " << readBuffer << endl;
    
    }

 
    return 0;
}